package com.benyq.networkprogramme.base.adapter

import android.content.Context
import android.support.v7.widget.RecyclerView
import android.util.SparseArray
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.benyq.networkprogramme.base.glide.GlideCircleTransform
import com.bumptech.glide.Glide

/**
 * @author benyq
 * @date 18-9-14 12:14
 * @E-mail： 1520063035@qq.com
 * @description
 */
open class BenyqRVHolder(context: Context, itemView: View): RecyclerView.ViewHolder(itemView) {

    private val mViews = SparseArray<View>()

    private val mConvertView: View = itemView
    private val mLayoutId: Int = 0
    protected var mContext: Context = context

    fun <V : View> getView(viewId: Int): V {
        var view: View? = mViews.get(viewId)
        if (view == null) {
            view = mConvertView.findViewById(viewId)
            mViews.put(viewId, view)
        }
        return view as V
    }


    fun setText(viewId: Int, value: String): BenyqRVHolder {
        val view = getView<TextView>(viewId)
        view.text = value
        return this
    }

    fun setVisibility(viewId: Int, flag: Boolean): BenyqRVHolder{
        val view = getView<View>(viewId)
        view.visibility = if (flag) View.VISIBLE else View.GONE
        return this
    }

    fun setImageUrl(viewId: Int, imgUrl: String): BenyqRVHolder {
        val view = getView<ImageView>(viewId)
        Glide.with(mContext).load(imgUrl).into(view)
        return this
    }

    fun setCircleImageUrl(viewId: Int, imgUrl: String): BenyqRVHolder {
        val view = getView<ImageView>(viewId)
        Glide.with(mContext).load(imgUrl).transform(GlideCircleTransform(mContext)).into(view)
        return this
    }
}